package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"strconv"
)

type DeviceHistoryEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	DeviceId   int64       // 设备厂商名称
	ObjectName string      // 设备类型名
	ParamValue interface{} // 设备名称
}

type deviceHistoryPo struct {
	Id         int64  `json:"id" db:"id"`                  // 对象id
	CreateTime int64  `json:"createTime" db:"create_time"` // 创建时间
	DeviceId   int64  `json:"deviceId" db:"device_id"`     // 设备厂商名称
	ObjectName string `json:"objectName" db:"object_name"` // 设备类型名
	ParamType  string `json:"ParamType" db:"param_type"`   // 设备名称
	ParamValue string `json:"ParamValue" db:"param_value"` // 设备名称
}

func (e *DeviceHistoryEntity) Type() string {
	return "DeviceHistoryEntity"
}

func (e *DeviceHistoryEntity) TableName() string {
	return "tb_device_history"
}

func (e *DeviceHistoryEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceHistoryEntity) Instance() *DeviceHistoryEntity {
	return &DeviceHistoryEntity{
		BaseEntity: new(commEntity.BaseEntity),
	}
}

func (e *DeviceHistoryEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceHistoryEntity) NewPo() interface{} {
	return &deviceHistoryPo{}
}

func (e *DeviceHistoryEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceHistoryEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, strconv.FormatInt(e.BaseEntity.Id, 10))
	return p
}

func (e *DeviceHistoryEntity) BuildByJson(jsn *string) error {
	var po deviceHistoryPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceHistoryEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceHistoryEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceHistoryPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.DeviceId = po.DeviceId
	ptr.ObjectName = po.ObjectName

	if po.ParamType == "Integer" {
		if v, err := strconv.ParseInt(po.ParamValue, 10, 32); err == nil {
			ptr.ParamValue = int32(v)
		}
	}
	if po.ParamType == "Short" {
		if v, err := strconv.ParseInt(po.ParamValue, 10, 32); err == nil {
			ptr.ParamValue = int32(v)
		}
	}
	if po.ParamType == "Long" {
		if v, err := strconv.ParseInt(po.ParamValue, 10, 64); err == nil {
			ptr.ParamValue = v
		}
	}
	if po.ParamType == "Float" {
		if v, err := strconv.ParseFloat(po.ParamValue, 32); err == nil {
			ptr.ParamValue = float32(v)
		}
	}
	if po.ParamType == "Double" {
		if v, err := strconv.ParseFloat(po.ParamValue, 64); err == nil {
			ptr.ParamValue = v
		}
	}

}

func (e *DeviceHistoryEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceHistoryPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.DeviceId = ptr.DeviceId
	po.ObjectName = ptr.ObjectName

	data := ptr.ParamValue

	if value, ok := data.(float64); ok {
		po.ParamType = "Double"
		po.ParamValue = strconv.FormatFloat(value, 'f', -1, 64)
	}
	if value, ok := data.(float32); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatFloat(float64(value), 'f', -1, 32)
	}
	if value, ok := data.(int64); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(value, 10)
	}
	if value, ok := data.(int32); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}
	if value, ok := data.(int16); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}
	if value, ok := data.(int8); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}
	if value, ok := data.(uint64); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}
	if value, ok := data.(uint32); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}
	if value, ok := data.(uint16); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}
	if value, ok := data.(uint8); ok {
		po.ParamType = "Float"
		po.ParamValue = strconv.FormatInt(int64(value), 10)
	}

	return &po
}

func (e *DeviceHistoryEntity) IsValue(data interface{}) bool {
	if _, ok := data.(float64); ok {
		return true
	}
	if _, ok := data.(float32); ok {
		return true
	}
	if _, ok := data.(int64); ok {
		return true
	}
	if _, ok := data.(int32); ok {
		return true
	}
	if _, ok := data.(int16); ok {
		return true
	}
	if _, ok := data.(int8); ok {
		return true
	}
	if _, ok := data.(uint64); ok {
		return true
	}
	if _, ok := data.(uint32); ok {
		return true
	}
	if _, ok := data.(uint16); ok {
		return true
	}
	if _, ok := data.(uint8); ok {
		return true
	}

	return false
}
